<script setup>
import { ref } from 'vue'

// 控制列表显示状态
const showProducts = ref(false)
// 切换显示状态的方法
const toggleProducts = () => {
  showProducts.value = !showProducts.value
}
// 待办事项列表（带初始示例数据）
const todoList = ref([
  { id: 1, text: '学习Vue', completed: false },
  { id: 2, text: '完成练习', completed: false }
])
const newTodo = ref('')

// 新增待办事项（带非空校验）
const addTodo = () => {
  if (newTodo.value.trim()) {
    todoList.value.push({
      id: Date.now(), // 使用时间戳作为唯一ID
      text: newTodo.value.trim(),
      completed: false
    })
    newTodo.value = '' // 清空输入框
  }
}

// 删除待办事项
const deleteTodo = (id) => {
  todoList.value = todoList.value.filter(item => item.id !== id)
}
</script>

<template>
  <div class="container">
    <h1>待办事项</h1>
    <br>
  </div>
  <div class="todo-container">

    <div class="input-group">
      <input v-model="newTodo"
          @keyup.enter="addTodo"
          placeholder="输入待办事项"
          class="todo-input"/>
      <button @click="addTodo" class="add-btn">添加</button>
    </div>


    <div v-if="todoList.length" class="todo-list">
      <div v-for="item in todoList" :key="item.id" class="todo-item">
        <span>{{ item.text }}</span>
        <button @click="deleteTodo(item.id)" class="delete-btn">
          删除
        </button>
      </div>
    </div>
    <p v-else class="empty-tip">暂无待办事项</p>
  </div>
</template>

<style scoped>
.container {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  font-family: Arial, sans-serif;
}

h1 {
  text-align: center;
  color: #333;
  margin-bottom: 20px;
}

.input-group {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.todo-input {
  flex: 1;
}

.add-btn {
  white-space: nowrap;
}

.todo-list {
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  overflow: hidden;
}

.todo-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid #f0f0f0;
  background: white;
}

.todo-item:last-child {
  border-bottom: none;
}

.todo-item span {
  flex: 1;
}

.empty-tip {
  text-align: center;
  color: #888;
  font-style: italic;
  margin-top: 20px;
}
</style>
